﻿using System.Collections.Generic;

namespace MyMovies
{
    internal class MediaInfoParser
    {
        public static MediaInfo mediainfo = null;
        public static MediaInfoData mediaInfoData = null;

        public static void Parse(int ID, string filename)
        {
            if (mediainfo == null)
            {
                mediainfo = new MediaInfo();
                mediaInfoData = new MediaInfoData();
            }

            if (mediainfo.Open(filename) > 0)
            {
                mediaInfoData.Clear();

                ReadVideoData(ID);
                TableVideoFileInfo.Insert(ID, mediaInfoData.Video);

                int stream = 0;
                while (ReadAudioData(ID, stream))
                {
                    TableAudioFileInfo.Insert(ID, stream, mediaInfoData.Audio[stream]);
                    stream++;
                }
                mediainfo.Close();
            }
        }

        private static void ReadVideoData(int ID)
        {
            int result = 0;
            mediaInfoData.Video.Width = int.TryParse(mediainfo.Get(StreamKind.Video, 0, "Width"), out result)
                                            ? result
                                            : 0;

            mediaInfoData.Video.Height = int.TryParse(mediainfo.Get(StreamKind.Video, 0, "Height"), out result)
                                            ? result
                                            : 0;
            mediaInfoData.Video.Duration = int.TryParse(mediainfo.Get(StreamKind.Video, 0, "Duration"), out result)
                                            ? result
                                            : 0;
            mediaInfoData.Video.Aspect = mediainfo.Get(StreamKind.Video, 0, "AspectRatio");
            mediaInfoData.Video.Format = mediainfo.Get(StreamKind.Video, 0, "Format");
        }

        private static bool ReadAudioData(int ID, int stream)
        {
            if (mediainfo.Get(StreamKind.Audio, stream, "Format") == "")
                return false;

            if (mediaInfoData.Audio.Count == stream)
                mediaInfoData.Audio.Add(new MediaInfoAudio());

            try
            {
                mediaInfoData.Audio[stream].Format = mediainfo.Get(StreamKind.Audio, stream, "Format");
                mediaInfoData.Audio[stream].Language = mediainfo.Get(StreamKind.Audio, stream, "Language");
                mediaInfoData.Audio[stream].SamplingRate = mediainfo.Get(StreamKind.Audio, stream, "SamplingRate");
                mediaInfoData.Audio[stream].BitRate = mediainfo.Get(StreamKind.Audio, stream, "BitRate");
                mediaInfoData.Audio[stream].Channel = int.Parse(mediainfo.Get(StreamKind.Audio, stream, "Channels"));
            }
            catch
            { }
            return true;
        }
    }

    public class MediaInfoVideo
    {
        public int Width { get; set; }
        public int Height { get; set; }
        public int Duration { get; set; }
        public string Aspect { get; set; }
        public string Format { get; set; }

        public void Clear()
        {
            Width = Height = Duration = 0;
            Aspect = Format = string.Empty;
        }
    }

    public class MediaInfoAudio
    {
        public string Format { get; set; }
        public string Language { get; set; }
        public string SamplingRate { get; set; }
        public string BitRate { get; set; }
        public int Channel { get; set; }
    }

    public class MediaInfoData
    {
        public MediaInfoVideo Video { get; private set; }
        public List<MediaInfoAudio> Audio { get; private set; }

        public MediaInfoData()
        {
            Video = new MediaInfoVideo();
            Audio = new List<MediaInfoAudio>();
        }

        public void Clear()
        {
            Video.Clear();
            Audio.Clear();
        }
    }
}